package com.iflytek.autofly.setting.biz.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

/**
 * <b>Description: </b>用于innerlistview作为radiogroup时的adapter<br/>
 * <b>Change History:</b><br/>
 * <table border="1">
 * <tr>
 * <td><b>user</b></td>
 * <td><b>date</b></td>
 * <td><b>time</b></td>
 * <td><b>reason</b></td>
 * </tr>
 * <tr>
 * <td>JunLuan@iflytek.com</td>
 * <td>2015/11/19</td>
 * <td>17:46</td>
 * <td>创建此文件</td>
 * </tr>
 * </table>
 *
 * @author JunLuan@iflytek.com
 */
public class SpAdpt<V extends SpAdprViewHolder, B extends SpAdptBean> extends BaseAdapter {

    private final int mViewId;
    ArrayList<B> mItems = new ArrayList<B>();
    private Context mContext;
    private V mFactory;
    private int position = -1;
    private int status = 0;

    public SpAdpt(Context ctx, V factory, int viewId) {
        mContext = ctx;
        mFactory = factory;
        mViewId = viewId;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return mItems.get(position).getId();
    }

    public Object getItemById(int id) {
        for (int i = 0; i < mItems.size(); i++) {
            if (mItems.get(i).getId() == id) {
                return mItems.get(i);
            }
        }
        return null;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final V holder;
        View view;
        final B bean = mItems.get(position);
        /*if (convertView == null || convertView.getTag() == null) {
            view = LayoutInflater.from(mContext).inflate(mViewId, null);
            holder = (V) mFactory.newInstance();
            holder.initView(view);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (V) view.getTag();
        }*/


        view = LayoutInflater.from(mContext).inflate(mViewId, null);
        holder = (V) mFactory.newInstance();
        holder.initView(view);



        holder.updateView(bean);
        if (this.position != -1 && this.position == position &&
                holder instanceof CommonRadioButtonHolder) {
            holder.setStatus(status);
        }
        return view;
    }

    public void addItem(B spAdptBean) {
        mItems.add(spAdptBean);
    }

    public void setItemStatus(int position, int status) {
        this.position = position;
        this.status = status;
    }

}
